package Resizers;

import org.eclipse.swt.graphics.*;

import Logs.Logger;
import SeamCarving.DynamicEnergyMap;
import Utils.Functions;

import Drawers.Drawer;

public class SeamLiveResizer implements Resizer 
{
	private DynamicEnergyMap _map;
	private boolean _isVertical; // if is true the picture will grow in height
	private Point _maxSize;
	
	public SeamLiveResizer(boolean isVertical)
	{
		_isVertical = isVertical;
	}
	
	public void Init(ImageData originalImage, Point maxSize) 
	{
		if (originalImage == null)
			return;
	
		if (_isVertical)
			_maxSize = new Point(Math.min(maxSize.x, originalImage.width), maxSize.y);
		else
			_maxSize = new Point(maxSize.x, Math.min(maxSize.y, originalImage.height));
			
		_map = new DynamicEnergyMap(originalImage);
		_map.InitSeamVector(!_isVertical, _isVertical ? _maxSize.y : _maxSize.x);
	}

	public Point CanResize(Point requestedSize)
	{
		if (_maxSize == null)
			throw new NullPointerException("Please initilize (using Init()) the resizer first.");
		
		if (_isVertical)
			return new Point(_map.CurrentSize().x, Math.min(_maxSize.y, requestedSize.y));
		else
			return new Point(Math.min(_maxSize.x, requestedSize.x), _map.CurrentSize().y);
	}
	
	public void Resize(ImageData orginalImage, Drawer drawer, Point trgSize)
	{
		if (!trgSize.equals(CanResize(trgSize)))
			throw new IndexOutOfBoundsException("TrgSize is not a vaild size. Please pass through CanResize() first.");
			
		drawer.Start(trgSize, Functions.ClearPalette());
		
		_map.makeSize(trgSize, !_isVertical);
		
		_map.draw(drawer, trgSize);
		drawer.Finish();
	}
	

	public void SetLogger(Logger log)
	{
		
	}
}
